package com.lecyon.farm.vo.sync;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.lecyon.farm.util.StringHelper;
import com.lecyon.farm.vo.request.Mercator;
import lombok.Getter;
import org.apache.commons.collections.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @author Yao Zheng
 * @Date 2020/4/29 18:12
 */
public class Geometry implements Serializable {

    @Getter
    private String type = "Multipolygon";

    @Getter
    private List<List<List<Double[]>>> coordinates;

    private StringBuilder geometryStringBuilder;

    private String initStartStr = "{" +
            "    \"type\": \"MultiPolygon\"," +
            "    \"coordinates\": [[[";

    private String initEndStr = " ]]]}";

    private String gps;

    private List<Mercator> mercatorList;

    public void setGps(String gps) {
        this.gps = gps;
        if (StringHelper.isNotEmpty(gps)) {
            mercatorList = JSONObject.parseArray(gps, Mercator.class);
            if (CollectionUtils.isNotEmpty(mercatorList)) {
                geometryStringBuilder = new StringBuilder();
                geometryStringBuilder.append(initStartStr);
                mercatorList.stream().forEach(item -> geometryStringBuilder.append(item.getMercatorValue()).append(","));
                geometryStringBuilder.deleteCharAt(geometryStringBuilder.lastIndexOf(","));
                geometryStringBuilder.append(initEndStr);
                coordinates = Lists.newLinkedList();
                List<List<Double[]>> list1 = Lists.newLinkedList();
                List<Double[]> list2 = Lists.newLinkedList();
                for (Mercator mercator : mercatorList) {
                    Double[] doubles = new Double[2];
                    doubles[0] = mercator.getLon().doubleValue();
                    doubles[1] = mercator.getLat().doubleValue();
                    list2.add(doubles);
                }
                list1.add(list2);
                coordinates.add(list1);
            }
        }
    }

}
